package com.hzmy.zm.dropdonwdemo.view;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


import com.hzmy.zm.dropdonwdemo.R;
import com.hzmy.zm.dropdonwdemo.adapter.FilterCenterAdapter;
import com.hzmy.zm.dropdonwdemo.adapter.FilterLeftAdapter;
import com.hzmy.zm.dropdonwdemo.adapter.FilterOneAdapter;
import com.hzmy.zm.dropdonwdemo.adapter.FilterRightAdapter;
import com.hzmy.zm.dropdonwdemo.model.FilterData;
import com.hzmy.zm.dropdonwdemo.model.FilterEntity;
import com.hzmy.zm.dropdonwdemo.model.FilterThreeEntity;

/**
 * 筛选条件自定义view
 */
public class FilterView extends LinearLayout implements View.OnClickListener {

    TextView tvCategory;
    ImageView ivCategoryArrow;
    TextView tvSort;
    ImageView ivSortArrow;
    TextView tvFilter;
    ImageView ivFilterArrow;
    LinearLayout llCategory;
    LinearLayout llSort;
    LinearLayout llFilter;
    ListView lvLeft;
    ListView lvCenter;
    ListView lvRight;
    LinearLayout llHeadLayout;
    LinearLayout llContentListView;

    View viewMaskBg;
    private Context mContext;
    private Activity mActivity;
    private boolean isStickyTop = false; // 是否吸附在顶部
    private boolean isShowing = false;
    private int filterPosition = -1;
    private int panelHeight;

    private FilterData filterData;
    private FilterThreeEntity selectedCategoryEntity; // 被选择的分类项
    FilterEntity selectedLevelTow; //二级
    FilterEntity selectedLevelThree; //三级

    private FilterEntity selectedSortEntity; // 被选择的排序项

    private FilterEntity selectedFilterEntity; // 被选择的筛选项
    private FilterLeftAdapter leftAdapter;//分类选项左侧
    private FilterCenterAdapter centerAdapter; //中间分类选项
    private FilterRightAdapter rightAdapter;
    private FilterOneAdapter sortAdapter; //排序选项
    private FilterOneAdapter filterAdapter;

    public FilterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public FilterView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        View rootView = LayoutInflater.from(context).inflate(R.layout.view_filter_layout, this);
        initView(rootView);
        initListener();
    }


    private void initView(View rootView) {
        tvCategory = (TextView) rootView.findViewById(R.id.tv_category);
        ivCategoryArrow = (ImageView) rootView.findViewById(R.id.iv_category_arrow);
        tvSort = (TextView) rootView.findViewById(R.id.tv_sort);
        ivSortArrow = (ImageView) rootView.findViewById(R.id.iv_sort_arrow);
        tvFilter = (TextView) rootView.findViewById(R.id.tv_filter);
        ivFilterArrow = (ImageView) rootView.findViewById(R.id.iv_filter_arrow);

        llCategory = (LinearLayout) rootView.findViewById(R.id.ll_category);
        llSort = (LinearLayout) rootView.findViewById(R.id.ll_sort);
        llFilter = (LinearLayout) rootView.findViewById(R.id.ll_filter);

        lvLeft = (ListView) rootView.findViewById(R.id.lv_left);
        lvCenter = (ListView) rootView.findViewById(R.id.lv_center);
        lvRight = (ListView) rootView.findViewById(R.id.lv_right);

        llHeadLayout = (LinearLayout) rootView.findViewById(R.id.ll_head_layout);
        llContentListView = (LinearLayout) rootView.findViewById(R.id.ll_content_list_view);
        viewMaskBg = rootView.findViewById(R.id.view_mask_bg);

        viewMaskBg.setVisibility(GONE);
        llContentListView.setVisibility(GONE);
    }

    private void initListener() {
        llCategory.setOnClickListener(this);
        llSort.setOnClickListener(this);
        llFilter.setOnClickListener(this);
        viewMaskBg.setOnClickListener(this);
        llContentListView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_category://分类
                filterPosition = 0;
                showFilterLayout(filterPosition);
                if (onFilterClickListener != null) {
                    onFilterClickListener.onFilterClick(filterPosition);
                }
                break;
            case R.id.ll_sort://排序
                filterPosition = 1;
                showFilterLayout(filterPosition);
                if (onFilterClickListener != null) {
                    onFilterClickListener.onFilterClick(filterPosition);
                }
                break;
            case R.id.ll_filter://筛选
                filterPosition = 2;
                showFilterLayout(filterPosition);
                if (onFilterClickListener != null) {
                    onFilterClickListener.onFilterClick(filterPosition);
                }
                break;
            case R.id.view_mask_bg://透明遮罩
                hide();
                break;
        }

    }

    // 复位筛选的显示状态
    public void resetFilterStatus() {
        tvCategory.setTextColor(mContext.getResources().getColor(R.color.font_black_2));
        ivCategoryArrow.setImageResource(R.mipmap.home_down_arrow);

        tvSort.setTextColor(mContext.getResources().getColor(R.color.font_black_2));
        ivSortArrow.setImageResource(R.mipmap.home_down_arrow);

        tvFilter.setTextColor(mContext.getResources().getColor(R.color.font_black_2));
        ivFilterArrow.setImageResource(R.mipmap.home_down_arrow);
    }

    // 复位所有的状态
    public void resetAllStatus() {
        resetFilterStatus();
        hide();
    }

    /**
     * 显示筛选布局
     * 1.重置筛选布局样式
     * 2.设置筛选条目的数据
     *
     * @param position
     */
    public void showFilterLayout(int position) {
        resetFilterStatus();
        switch (position) {
            case 0:
                setCategoryAdapter();
                break;
            case 1:
                setSortAdapter();
                break;
            case 2:
                setFilterAdapter();
                break;
        }

        if (isShowing) return;
        show();
    }


    /**
     * 设置分类数据
     * 1.顶部字体图片样式的改变
     * 2.listview的显示
     * 3.默认选中第一个条目
     * 4.
     */
    private void setCategoryAdapter() {
        tvCategory.setTextColor(mActivity.getResources().getColor(R.color.orange));
        ivCategoryArrow.setImageResource(R.mipmap.home_up_arrow);
        lvLeft.setVisibility(VISIBLE);
        lvRight.setVisibility(VISIBLE);
        lvCenter.setVisibility(VISIBLE);

        // 左边列表视图
        leftAdapter = new FilterLeftAdapter(mContext, filterData.getCategory());
        lvLeft.setAdapter(leftAdapter);
        //从保存的数据中获取选择的数据
        if (selectedCategoryEntity == null) {
            selectedCategoryEntity = filterData.getCategory().get(0);
        }
        leftAdapter.setSelectedEntity(selectedCategoryEntity);
        //当一级菜单不为null时候去设置二级三级目录
        if (selectedCategoryEntity != null){
            centerAdapter = new FilterCenterAdapter(mContext, selectedCategoryEntity.getLevelTowList());
            if (selectedCategoryEntity.getSelectedLevelTow() == null){
                selectedCategoryEntity.setSelectedLevelTow(selectedCategoryEntity.getLevelTowList().get(0));
            }
            centerAdapter.setSelectedEntity(selectedCategoryEntity.getSelectedLevelTow());

            rightAdapter = new FilterRightAdapter(mContext, selectedCategoryEntity.getLevelThreeList());
            if (selectedCategoryEntity.getSelectLevelThree() == null){
                selectedCategoryEntity.setSelectLevelThree(selectedCategoryEntity.getLevelThreeList().get(0));
            }
            rightAdapter.setSelectedEntity(selectedCategoryEntity.getSelectLevelThree());
        }else{
            centerAdapter = new FilterCenterAdapter(mContext, filterData.getCategory().get(0).getLevelTowList());
            rightAdapter = new FilterRightAdapter(mContext, filterData.getCategory().get(0).getLevelThreeList());
            centerAdapter.setSelectedEntity(filterData.getCategory().get(0).getLevelTowList().get(0));
            rightAdapter.setSelectedEntity(filterData.getCategory().get(0).getLevelThreeList().get(0));
        }
        lvCenter.setAdapter(centerAdapter);
        lvRight.setAdapter(rightAdapter);

        lvLeft.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //1.重新设置分类选中entity
                selectedCategoryEntity = filterData.getCategory().get(position);
                leftAdapter.setSelectedEntity(selectedCategoryEntity);
                lvLeft.setSelection(position);

                //2.一级菜单影响二级三级菜单
                selectedCategoryEntity.setSelectedLevelTow(selectedCategoryEntity.getLevelTowList().get(0));
                centerAdapter.setSelectedEntity(selectedCategoryEntity.getSelectedLevelTow());
                lvCenter.setSelection(0);

                selectedCategoryEntity.setSelectLevelThree(selectedCategoryEntity.getLevelThreeList().get(0));
                rightAdapter.setSelectedEntity(selectedCategoryEntity.getSelectLevelThree());
                lvRight.setSelection(0);
            }
        });
        lvCenter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                selectedCategoryEntity.setSelectedLevelTow(selectedCategoryEntity.getLevelTowList().get(position));
                centerAdapter.setSelectedEntity(selectedCategoryEntity.getSelectedLevelTow());
                lvCenter.setSelection(position);
                //2.影响三级菜单
                selectedCategoryEntity.setSelectLevelThree(selectedCategoryEntity.getLevelThreeList().get(0));
                rightAdapter.setSelectedEntity(selectedCategoryEntity.getSelectLevelThree());
                lvRight.setSelection(0);
            }
        });
        lvRight.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectedCategoryEntity.setSelectLevelThree(selectedCategoryEntity.getLevelThreeList().get(position));
                rightAdapter.setSelectedEntity(selectedCategoryEntity.getSelectLevelThree());//设置左边菜单选择对呀的右边数据
                lvRight.setSelection(position);
                hide();
                if (onItemCategoryClickListener != null) {
                    onItemCategoryClickListener.onItemCategoryClick(selectedCategoryEntity);
                }
                tvCategory.setText(selectedCategoryEntity.getSelectLevelThree().getKey());
            }
        });
    }


    /**
     * 设置排序数据
     * 1.重置样式
     * 2.显示右边的listview
     * 3.设置适配器
     * 4.点击后设置点击的条目，回调
     */

    private void setSortAdapter() {
        tvSort.setTextColor(mActivity.getResources().getColor(R.color.orange));
        ivSortArrow.setImageResource(R.mipmap.home_up_arrow);
        lvLeft.setVisibility(GONE);
        lvCenter.setVisibility(GONE);
        lvRight.setVisibility(VISIBLE);
        sortAdapter = new FilterOneAdapter(mContext, filterData.getSorts());
        lvRight.setAdapter(sortAdapter);
        lvRight.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectedSortEntity = filterData.getSorts().get(position);
                sortAdapter.setSelectedEntity(selectedSortEntity);
                hide();
                if (onItemSortClickListener != null) {
                    onItemSortClickListener.onItemSortClick(selectedSortEntity);
                }
            }
        });
    }

    // 设置筛选数据
    private void setFilterAdapter() {
        tvFilter.setTextColor(mActivity.getResources().getColor(R.color.orange));
        ivFilterArrow.setImageResource(R.mipmap.home_up_arrow);
        lvLeft.setVisibility(GONE);
        lvCenter.setVisibility(GONE);
        lvRight.setVisibility(VISIBLE);
        filterAdapter = new FilterOneAdapter(mContext, filterData.getFilters());
        lvRight.setAdapter(filterAdapter);
        lvRight.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectedFilterEntity = filterData.getFilters().get(position);
                filterAdapter.setSelectedEntity(selectedFilterEntity);
                hide();
                if (onItemFilterClickListener != null) {
                    onItemFilterClickListener.onItemFilterClick(selectedFilterEntity);
                }
            }
        });
    }

    /**
     * 动画显示
     * 1.isShow置为true
     * 2.遮罩背景显示
     * 3.listview显示
     * 4.动画执行
     */
    private void show() {
        isShowing = true;
        viewMaskBg.setVisibility(VISIBLE);
        llContentListView.setVisibility(VISIBLE);
        llContentListView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                llContentListView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                panelHeight = llContentListView.getHeight();
//                Toast.makeText(mContext, "高度：" + panelHeight , Toast.LENGTH_LONG).show();
                ObjectAnimator.ofFloat(llContentListView, "translationY", -panelHeight, 0).setDuration(200).start();
            }
        });
    }

    /**
     * 隐藏动画
     * 收起遮罩
     * 1.遮罩展示为fasle
     * 2.重置筛选条目的样式
     * 3.遮罩背景隐藏
     * 3.listview条目选择动画收起
     */
    public void hide() {
        isShowing = false;
        resetFilterStatus();
        viewMaskBg.setVisibility(View.GONE);
        ObjectAnimator.ofFloat(llContentListView, "translationY", 0, -panelHeight).setDuration(200).start();
    }

    // 是否吸附在顶部
    public void setStickyTop(boolean stickyTop) {
        isStickyTop = stickyTop;
    }

    // 设置筛选数据
    public void setFilterData(Activity activity, FilterData filterData) {
        this.mActivity = activity;
        this.filterData = filterData;
    }

    // 是否显示
    public boolean isShowing() {
        return isShowing;
    }

    // 筛选视图点击
    private OnFilterClickListener onFilterClickListener;

    public void setOnFilterClickListener(OnFilterClickListener onFilterClickListener) {
        this.onFilterClickListener = onFilterClickListener;
    }

    public interface OnFilterClickListener {
        void onFilterClick(int position);
    }

    // 分类Item点击
    private OnItemCategoryClickListener onItemCategoryClickListener;

    public void setOnItemCategoryClickListener(OnItemCategoryClickListener onItemCategoryClickListener) {
        this.onItemCategoryClickListener = onItemCategoryClickListener;
    }

    public interface OnItemCategoryClickListener {
        void onItemCategoryClick(FilterThreeEntity entity);
    }

    // 排序Item点击
    private OnItemSortClickListener onItemSortClickListener;

    public void setOnItemSortClickListener(OnItemSortClickListener onItemSortClickListener) {
        this.onItemSortClickListener = onItemSortClickListener;
    }

    public interface OnItemSortClickListener {
        void onItemSortClick(FilterEntity entity);
    }

    // 筛选Item点击
    private OnItemFilterClickListener onItemFilterClickListener;

    public void setOnItemFilterClickListener(OnItemFilterClickListener onItemFilterClickListener) {
        this.onItemFilterClickListener = onItemFilterClickListener;
    }

    public interface OnItemFilterClickListener {
        void onItemFilterClick(FilterEntity entity);
    }

    /**
     * 设置筛选条目的名称
     *
     * @param text
     */
    public void setFilterTitle(String... text) {
        if (text.length > 3)
            return;
        tvCategory.setText(text[0]);
        tvSort.setText(text[1]);
        tvFilter.setText(text[2]);
    }

}
